//定义影院相关的接口
const express = require('express')
const router = express.Router()
const response = require('../pool/Response')
//引入mysql连接池
const pool = require('../pool/pool')
//引入joi模块
const joi = require('joi')

//添加排片计划接口
router.post('/plan/add',(req,res) => {
    let {cinema_id,cinema_room_id,movie_id,showingon_date,showingon_time,status,price} = req.body
    let schema = joi.object({
        cinema_id:joi.string().required(),
        cinema_room_id:joi.string().required(),
        movie_id:joi.string().required(),
        showingon_date:joi.string().required(),
        showingon_time:joi.string().required(),
        status:joi.number().required(),
        price:joi.number().required(),
    })
    let {error,value} = schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return;
    }
    pool.query(
        'insert into showingon_plan (cinema_id,cinema_room_id,movie_id,showingon_date,showingon_time,status,price) values(?,?,?,?,?,?,?)',
    [cinema_id,cinema_room_id,movie_id,showingon_date,showingon_time,status,price],(err,result)=>{
        if(err){
            res.send(response.error(500,err))
            throw err;
        }
        res.send(response.ok())
    })
})
//通过放映厅id查找排片计划
router.get('/plans/roomid',(req,res) => {
    let {room_id} = req.query
    let schema = joi.object({
        room_id:joi.number().required()
    })
    let {error,value} = schema.validate(req.query)
    if(error){
        res.send(response.error(400,error))
        return
    }
    let now = new Date()
    let year = now.getFullYear();
    let month = now.getMonth() + 1 < 10 ? '0'+ (now.getMonth()+1) : now.getMonth
    let day = now.getDate() < 10 ? '0' + now.getDate() : now.getDate()
    let time = `${year}-${month}-${day}`
   pool.query(`select
     sp.id plan_id,
     sp.cinema_id cinema_id,
     sp.cinema_room_id cinema_room_id,
     sp.movie_id movie_id,
     mi.title title,
     sp.showingon_date showingon_date,
     sp.showingon_time showingon_time,
     sp.status status,
     sp.price price
     from 
     showingon_plan sp join movie_info mi on sp.movie_id=mi.id
     where
     cinema_room_id=? and showingon_date>?
     order by
     showingon_date,showingon_time  
   `,[room_id,time],(error,result) => {
    if(error){
        res.send(response.error(500,error))
        throw error
    }
    res.send(response.ok(result))
   })
})


//删除拍片计划
router.post('/plan/del',(req,res) => {
    let {id} = req.body
    let schema = joi.object({
        id:joi.number().required()
    })
    let {error,value} = schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return
    }
    pool.query('delete from showingon_plan where id=?',[id],(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error
        }
        res.send(response.ok())
    })
})
//将排片计划设置为未发布状态
router.post('/plan/no-publish',(req,res) => {
    let {id} = req.body
    let schema = joi.object({
        id:joi.number().required()
    })
    let {error,value} = schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return
    }
    pool.query('update showingon_plan set status=0 where id = ?',[id],(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error
        }
        res.send(response.ok())
    })
})

//将排片计划设置为发布状态
router.post('/plan/publish',(req,res) => {
    let {id} = req.body
    let schema = joi.object({
        id:joi.number().required()
    })
    let {error,value} = schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return
    }
    pool.query('update showingon_plan set status=1 where id = ?',[id],(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error
        }
        res.send(response.ok())
    })
})
module.exports = router